// 分类模块

import { topList } from '@/api/constants'
import { getHomeHeaderData } from '@/api/categoies'

export default {
    namespaced: true,
    state() {

        return {
            // 配合响应的数据，遍历成为对象
            list: topList.map(item => ({ name: item })),
            cateList: []
        }
    },

    // 数据加载成功后就需要修改list的数据
    mutations: {
        setList(state, payload) {
            state.list = payload
        },


        // 更改list中的状态
        show(state, payload) {
            const show = state.list.find(item => item.id == payload.id)
            show.open = true
        },


        // 隐藏
        hide(state, payload) {
            const hide = state.list.find(item => item.id == payload.id)
            hide.open = false
        },


        // 获取左侧边栏数据
        getcateList(state, payload) {
            state.cateList = state.list.find(item => item.id == payload)
        }
    },

    // 发送请求获得后端数据
    actions: {
        async getList(context) {
            let { result } = await getHomeHeaderData()
            result.forEach(item => {
                item.open = false
            })
            context.commit('setList', result)
        },
        // 异步处理隐藏
        getHide(context, payload) {
            setTimeout(() => {
                context.commit('hide', payload)
            }, 300);
        }
    }
}